package com.yuyou.fn.provider.impl;

import com.github.wens.mybatisplus.examples.Example;
import com.google.common.base.Function;
import com.google.common.collect.Collections2;
import com.yuyou.fn.api.CampusService;
import com.yuyou.fn.api.dto.CampusDto;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.educational.entity.Campus;
import com.yuyou.fn.educational.service.ICampusService;
import org.apache.commons.lang3.Validate;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class ExposeCampusServiceImpl implements CampusService {

    @Resource
    private ICampusService campusService;

    @Override
    public Collection<CampusDto> findCampusByCity(String city) {
        Validate.notNull(city,"城市不能为空");
        Example<Campus> example = Campus.createExample();
        example.createCriteria().andEqualTo("city",city );

        List<Campus> campus = campusService.findCampusByCity(city);

        if(campus == null ){
            return Collections.EMPTY_LIST;
        }

        return Collections2.transform(campus, new Function<Campus, CampusDto>() {
            @Override
            public CampusDto apply(Campus campus) {
                return toCampusDto(campus);
            }
        });
    }

    @Override
    public CampusDto findCampusById(Long campusId) {
        return toCampusDto(campusService.findById(campusId));
    }

    private CampusDto toCampusDto(Campus campus) {
        CampusDto campusDto = new CampusDto() ;
        BeanUtils.copyProperties(campusDto,campus);
        return campusDto;
    }

    @Override
    public Collection<CampusDto> findCampusByAreaTeamId(Long areaTeamId) {
        Validate.notNull(areaTeamId,"区域不能为空");
        Example<Campus> example = Campus.createExample();
        example.createCriteria().andEqualTo("areaTeamId",areaTeamId );

        List<Campus> campus = campusService.findCampusByAreaTeamId(areaTeamId);

        if(campus == null ){
            return Collections.EMPTY_LIST;
        }

        return Collections2.transform(campus, new Function<Campus, CampusDto>() {
            @Override
            public CampusDto apply(Campus campus) {
                return toCampusDto(campus);
            }
        });
    }
}
